package com.java.system.controller;

import com.java.system.entity.*;
import com.java.system.service.IEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  事件控制器
 * </p>
 *
 * @author xiaoman
 * @since 2023-07-12
 */
@RestController
@RequestMapping("/event")
public class EventController {
    @Autowired
    IEventService eventService;
    @GetMapping("/list")
    public  Result getEvent(){
        List<Event> eventList = eventService.getEvent();
        int size = eventList.size();
        if(!StringUtils.isEmpty(eventList)){
            Result result = Result.success("查询事件成功");
            result.put("data",eventList);
            result.put("size",size);
            return result;
        }
        else {
            return Result.error("查询事件失败");
        }
    }
    @PostMapping("/add")
    public Result insertEvent(@RequestBody Event event){
        eventService.insertEvent(event);
        return Result.success("增加事件成功");
    }
    @GetMapping("/{id}")
    public Result getEventById(@PathVariable("id")Integer id){
        Event event = eventService.getEventById(id);
        if(event!=null){
            Result result = Result.success("查询事件成功");
            result.put("data",event);
            return result;
        }
        else {
            return Result.error("查询事件失败");
        }
    }
    @PutMapping
    public Result updateEvent(@RequestBody Event event){
        eventService.updateEvent(event);
        return Result.success("修改事件信息成功");
    }
    @DeleteMapping("/{id}")
    public Result deleteEvent(@PathVariable("id")Integer id){
        eventService.deleteEvent(id);
        return Result.success("删除事件成功");
    }


    @GetMapping("/type/{id}")
    public Result type(@PathVariable("id")int id){
        TimeRange timeRange = TimeRange.valueOf(id);
        // 获取当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        LocalDateTime startTime = currentDateTime.minusHours(24);
        // 计算起始时间，减去24小时
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<EventType> eventList = eventService.selectByTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList)){
                    Result result = Result.success("24小时事件类型态势数据");
                    result.put("data",eventList);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<EventType> eventList1 = eventService.selectByTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList1)){
                    Result result = Result.success("一个周事件类型态势数据");
                    result.put("data",eventList1);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<EventType> eventList2 = eventService.selectByTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList2)){
                    Result result = Result.success("一个月事件类型态势数据");
                    result.put("data",eventList2);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<EventType> eventList3 = eventService.selectByTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList3)){
                    Result result = Result.success("6个月事件类型态势数据");
                    result.put("data",eventList3);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }

    }

    @GetMapping("/child/{id}")
    public Result child(@PathVariable("id")int id){
        // 获取当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 计算起始时间，减去24小时
        LocalDateTime startTime = currentDateTime.minusHours(24);
        TimeRange timeRange = TimeRange.valueOf(id);
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<ChildEvent> eventList = eventService.selectChild(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList)){
                    Result result = Result.success("24小时子事件态势数据");
                    result.put("data",eventList);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<ChildEvent> eventList1 = eventService.selectChild(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList1)){
                    Result result = Result.success("一个周子事件态势数据");
                    result.put("data",eventList1);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<ChildEvent> eventList2 = eventService.selectChild(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList2)){
                    Result result = Result.success("一个月子事件态势数据");
                    result.put("data",eventList2);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<ChildEvent> eventList3 = eventService.selectChild(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList3)){
                    Result result = Result.success("6个月子事件态势数据");
                    result.put("data",eventList3);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }

    }


    @GetMapping("/time/{id}")
    public Result time(@PathVariable("id")int id){
        // 获取当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 计算起始时间，减去24小时
        LocalDateTime startTime = currentDateTime.minusHours(24);
        TimeRange timeRange = TimeRange.valueOf(id);
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<EventTime> eventList = eventService.selectTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList)){
                    Result result = Result.success("24小时事件产生时间态势数据");
                    result.put("data",eventList);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<EventTime> eventList1 = eventService.selectTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList1)){
                    Result result = Result.success("一个周事件产生时间态势数据");
                    result.put("data",eventList1);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<EventTime> eventList2 = eventService.selectTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList2)){
                    Result result = Result.success("一个月事件产生时间态势数据");
                    result.put("data",eventList2);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<EventTime> eventList3 = eventService.selectTime(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList3)){
                    Result result = Result.success("6个月事件产生时间态势数据");
                    result.put("data",eventList3);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }

    }


    @GetMapping("/area/{id}")
    public Result area(@PathVariable("id")int id){
        // 获取当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 计算起始时间，减去24小时
        LocalDateTime startTime = currentDateTime.minusHours(24);
        TimeRange timeRange = TimeRange.valueOf(id);
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<EventArea> eventList = eventService.selectArea(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList)){
                    Result result = Result.success("24小时事件产生区域态势数据");
                    result.put("data",eventList);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<EventArea> eventList1 = eventService.selectArea(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList1)){
                    Result result = Result.success("一个周事件产生区域态势数据");
                    result.put("data",eventList1);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<EventArea> eventList2 = eventService.selectArea(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList2)){
                    Result result = Result.success("一个月事件产生区域态势数据");
                    result.put("data",eventList2);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<EventArea> eventList3 = eventService.selectArea(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList3)){
                    Result result = Result.success("6个月事件产生区域态势数据");
                    result.put("data",eventList3);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }

    }



    @GetMapping("/count/{id}")
    public Result count(@PathVariable("id")int id){
        // 获取当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 计算起始时间，减去24小时
        LocalDateTime startTime = currentDateTime.minusHours(24);
        TimeRange timeRange = TimeRange.valueOf(id);
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<Event> eventList = eventService.selectCount(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList)){
                    Result result = Result.success("24小时事件产生总数态势数据");
                    result.put("data",eventList);
                    result.put("count",eventList.size());
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<Event> eventList1 = eventService.selectCount(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList1)){
                    Result result = Result.success("一个周事件产生总数态势数据");
                    result.put("data",eventList1);
                    result.put("count",eventList1.size());
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<Event> eventList2 = eventService.selectCount(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList2)){
                    Result result = Result.success("一个月事件产生总数态势数据");
                    result.put("data",eventList2);
                    result.put("count",eventList2.size());
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<Event> eventList3 = eventService.selectCount(startTime,currentDateTime);
                if(!StringUtils.isEmpty(eventList3)){
                    Result result = Result.success("6个月事件产生总数态势数据");
                    result.put("data",eventList3);
                    result.put("count",eventList3.size());
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }

    }

    @GetMapping("/allEvent")
    public Result allEvent(@RequestParam(value = "type" ,required = false)String type,
                           @RequestParam(value = "areaId" ,required = false)Integer areaId,
                           @RequestParam(value = "id" ,required = false)Integer id){

        LocalDateTime currentDateTime = LocalDateTime.now();
        // 计算起始时间，减去24小时
        LocalDateTime startTime = currentDateTime.minusHours(24);
        TimeRange timeRange = TimeRange.valueOf(id);
        switch (timeRange) {
            case LAST_24_HOURS:
                // 执行近24小时查询
                List<ChildEvent> eventList = eventService.selectAllEvent(startTime,currentDateTime,type,areaId);
                if(!StringUtils.isEmpty(eventList)){
                    Result result = Result.success("24小时事件态势数据");
                    result.put("data",eventList);
                    int a=0,b=0,c=0;
                    Map<Object,Object> map=new HashMap<>();
                    for(ChildEvent event:eventList){
                        if(event.getType().equals("治安事件")){
                            a=a+event.getCount();
                        }
                        else if(event.getType().equals("流量事件")){
                            b=b+event.getCount();
                        }
                        else {
                            c=c+event.getCount();
                        }
                    }
                    result.put("count",a);
                    result.put("count1",b);
                    result.put("count2",c);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_WEEK:
                // 执行近一个周查询
                startTime=currentDateTime.minusWeeks(1);
                List<ChildEvent> eventList1 = eventService.selectAllEvent(startTime,currentDateTime,type,areaId);
                if(!StringUtils.isEmpty(eventList1)){
                    Result result = Result.success("一个周事件产生总数态势数据");
                    result.put("data",eventList1);
                    int a=0,b=0,c=0;
                    Map<Object,Object> map=new HashMap<>();
                    for(ChildEvent event:eventList1){
                        if(event.getType().equals("治安事件")){
                            a=a+event.getCount();
                        }
                        else if(event.getType().equals("流量事件")){
                            b=b+event.getCount();
                        }
                        else {
                            c=c+event.getCount();
                        }
                    }
                    result.put("count",a);
                    result.put("count1",b);
                    result.put("count2",c);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            case LAST_MONTH:
                // 执行近一个月查询
                startTime=currentDateTime.minusMonths(1);
                List<ChildEvent> eventList2 = eventService.selectAllEvent(startTime,currentDateTime,type,areaId);
                if(!StringUtils.isEmpty(eventList2)){
                    Result result = Result.success("一个月事件产生总数态势数据");
                    result.put("data",eventList2);
                    int a=0,b=0,c=0;
                    Map<Object,Object> map=new HashMap<>();
                    for(ChildEvent event:eventList2){
                        if(event.getType().equals("治安事件")){
                            a=a+event.getCount();
                        }
                        else if(event.getType().equals("流量事件")){
                            b=b+event.getCount();
                        }
                        else {
                            c=c+event.getCount();
                        }
                    }
                    result.put("count",a);
                    result.put("count1",b);
                    result.put("count2",c);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_MONTH6:
                startTime=currentDateTime.minusMonths(6);
                List<ChildEvent> eventList3 = eventService.selectAllEvent(startTime,currentDateTime,type,areaId);
                if(!StringUtils.isEmpty(eventList3)){
                    Result result = Result.success("6个月事件产生总数态势数据");
                    result.put("data",eventList3);
                    int a=0,b=0,c=0;
                    Map<Object,Object> map=new HashMap<>();
                    for(ChildEvent event:eventList3){
                        if(event.getType().equals("治安事件")){
                            a=a+event.getCount();
                        }
                        else if(event.getType().equals("流量事件")){
                            b=b+event.getCount();
                        }
                        else {
                            c=c+event.getCount();
                        }
                    }
                    result.put("count",a);
                    result.put("count1",b);
                    result.put("count2",c);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }
            case LAST_NULL:
                startTime=null;
                currentDateTime=null;
                List<ChildEvent> eventList4 = eventService.selectAllEvent(startTime,currentDateTime,type,areaId);
                if(!StringUtils.isEmpty(eventList4)){
                    Result result = Result.success("事件产生总数态势数据");
                    result.put("data",eventList4);
                    int a=0,b=0,c=0;
                    Map<Object,Object> map=new HashMap<>();
                    for(ChildEvent event:eventList4){
                        if(event.getType().equals("治安事件")){
                            a=a+event.getCount();
                        }
                        else if(event.getType().equals("流量事件")){
                            b=b+event.getCount();
                        }
                        else {
                            c=c+event.getCount();
                        }
                    }
                    result.put("count",a);
                    result.put("count1",b);
                    result.put("count2",c);
                    return result;
                }
                else {
                    return Result.error("查询失败");
                }

            default:
                // 处理无效的时间范围值
                return Result.error("失败");

        }
    }

}
